import { deployHttp } from '/@/utils/http/axios';
import {
  AppCardInfo,
  AppCardList,
  AppInfo,
  AppName,
  AppParams,
  AppProgram,
  AppSaveDto,
  AppSelectListItem,
  AppUser,
  CertifiedIntegration,
  CloudLinkInfo,
  deleteDto,
  ModuleModel,
  ModuleSave,
  RoleRelation,
  SubAppInfo,
  SubscribeApp,
  SyncIntegration,
} from './model/appModel';
import { UploadFileParams } from '/#/axios';
import { tenantHttp } from '../../../utils/http/axios/index';

enum Api {
  ALL_APP = '/v1/app/allApp',
  APP_SELECT_LIST = '/v1/app/authApps',
  APP_DISPLAY_LIST = '/v1/app/appPage',
  APP_ADD_OR_UPDATE = '/v1/app/addOrUpdate',
  APP_DELETE = '/v1/app/delete',

  APP_UPLOAD_LOGO_TEST = '/basic-api/saaslandingDeployment/v1/app/uploadTest',
  APP_UPLOAD_LOGO = '/basic-api/saaslandingDeployment/v1/app/logo/',
  SUB_APP_LIST = '/tenant/subAppList',
  APP_ACCOUNT = '/appAccount/listAppAccount',
  UPDATE_AUTH = '/appAccount/batchUserAuthApp',
  GET_SUBSCRIPTION_LIST = '/v1/app/getBizAppList',
  GET_SUBSCRIPTION_INFO = '/tenantApp/getSubscribedApp',
  GET_PACKAGE_LIST = '/tenantApp/listPackagesByAppId',
  SUBSCRIBE_APP = '/tenantApp/deployPackage',
  UNSUBSCRIBE_APP = '/tenantApp/release',
  GET_TASK_STATUS = '/tenantApp/getTaskStatus',
  GET_APP_INFO = '/tenantApp/getAppTemplate',
  UPDATE_LOGO = '/api/admin/saaslandingTenant/app/updateLogo',
  GET_APP_INFO_BY_ID = '/app/getAppById',

  GET_APP_DETAIL = '/v1/app/getDetail',
  GET_MODULE_LIST = '/v1/app/getModuleList',
  MODULE_ADD_OR_UPDATE = '/v1/app/createOrUpdateModule',
  GET_PROGRAM_LIST = '/v1/app/getAppProgramList',
  PROGRAM_ADD_OR_UPDATE = '/v1/app/createOrUpdateProgram',
  GET_CLOUD_LINK_LIST = '/v1/app/getCloudLinkList',
  CLOUD_LINK_ADD_OR_UPDATE = '/v1/app/createOrUpdateCloudLink',

  GET_CERTIFIED_INTEGRATION = '/v1/app/getCertifiedIntegration',
  UPDATE_CERTIFIED_INTEGRATION = '/v1/app/updateCertifiedIntegration',
  GET_SYNC_INTEGRATION = '/v1/app/getSyncIntegration',
  UPDATE_SYNC_INTEGRATION = '/v1/app/updateSyncIntegration',
  GET_SFS_LIST = '/v1/deployment/getSfsTurboShareType',
  CREATE_SFS_TURBO = '/deployment/createSfsTurbo',

  DELETE_MIRROR = '/v1/image/delete',
}

export const getAllApp = (params?: AppName) => {
  // App展示页，不分页获取应用列表
  return deployHttp.get<AppInfo[]>({ url: Api.ALL_APP, params });
};

export const getAppSelectList = async (params: AppParams) => {
  // 下拉框获取所有应用
  const res = await deployHttp.get<AppSelectListItem[]>({ url: Api.APP_SELECT_LIST, params });
  const result = res.map((item) => {
    return {
      label: item.name,
      value: item.id,
    };
  });
  return result;
};

export const getAppDisplaypList = (params?: AppName) => {
  // App展示页，不分页获取应用列表
  return deployHttp.post<AppInfo[]>({ url: Api.APP_DISPLAY_LIST, params });
};

export const addOrUpdateApp = (params: AppSaveDto) => {
  // 新增/编辑APP
  return deployHttp.post<string>({ url: Api.APP_ADD_OR_UPDATE, params });
};

export const deleteApp = (params: deleteDto) => {
  // 删除应用
  return deployHttp.get<string>({ url: Api.APP_DELETE, params });
};

export function logoUpdateTest(params: UploadFileParams, onUploadProgress: (progressEvent: ProgressEvent) => void) {
  // 上传文件
  return deployHttp.uploadFile({ url: Api.APP_UPLOAD_LOGO_TEST, onUploadProgress }, params);
}

export function logoUpdate(params: UploadFileParams, onUploadProgress: (progressEvent: ProgressEvent) => void) {
  // 上传LOGO应用ID
  return deployHttp.uploadFile({ url: Api.APP_UPLOAD_LOGO + params.data?.appId, onUploadProgress }, params);
}

export const getSubAppList = () => {
  return tenantHttp.get<SubAppInfo[]>({ url: Api.SUB_APP_LIST });
};

export const getAppAccount = (params: { tenantCode: string; appId: string }) => {
  return tenantHttp.get<AppUser[]>({ url: Api.APP_ACCOUNT, params });
};

export const updateAppAuth = async (params: { appId: string; appAuthForUsers: RoleRelation[] }) => {
  return await tenantHttp.post<string[]>({
    url: Api.UPDATE_AUTH,
    params,
  });
};

export const getSubscriptionList = async () => {
  const res = await deployHttp.get<AppCardList[]>({
    url: Api.GET_SUBSCRIPTION_LIST,
  });
  res.forEach((item) => {
    if (item.ifIntegrateThirdApp === 'true') {
      item.ifIntegrateThirdApp = true;
    } else {
      item.ifIntegrateThirdApp = false;
    }
    if (item.ifIntegrateCm === 'true') {
      item.ifIntegrateCm = true;
    } else {
      item.ifIntegrateCm = false;
    }
  });
  return res;
};
export const getSubscriptionInfo = (params: { appId: string }) => {
  return tenantHttp.get({ url: Api.GET_SUBSCRIPTION_INFO, params });
};
export const getPackageList = (params: { appId: string }) => {
  return tenantHttp.get({ url: Api.GET_PACKAGE_LIST, params });
};
export const subscribeApp = (data: Partial<SubscribeApp>) => {
  return tenantHttp.post({ url: Api.SUBSCRIBE_APP, data });
};
export const unsubscribeApp = (data: { appId: string; tenantCode: string }) => {
  return tenantHttp.post<boolean>({ url: Api.UNSUBSCRIBE_APP, data });
};
export const getTaskStatus = (params: { appId: string }) => {
  return tenantHttp.get({ url: Api.GET_TASK_STATUS, params });
};
export const getAppInfo = (params: { appId: string }) => {
  return tenantHttp.get({ url: Api.GET_APP_INFO, params });
};

export const updateLogo = (file: File, appId: string) => {
  return tenantHttp.uploadFile({ url: Api.UPDATE_LOGO }, { file, data: { appId } });
};
export const getAppById = (params: { appId: string }) => {
  return tenantHttp.get({ url: Api.GET_APP_INFO_BY_ID, params });
};
export const getModuleList = (params: { id: string }) => {
  return deployHttp.get<ModuleModel[]>({ url: Api.GET_MODULE_LIST, params });
};
export const createOrUpdateModule = (params: ModuleSave) => {
  return deployHttp.post<string>({ url: Api.MODULE_ADD_OR_UPDATE, params });
};

export const getAppProgramList = (params: { id: string }) => {
  return deployHttp.get<AppProgram[]>({ url: Api.GET_PROGRAM_LIST, params });
};
export const createOrUpdateProgram = (params: AppProgram) => {
  return deployHttp.post<string>({ url: Api.PROGRAM_ADD_OR_UPDATE, params });
};

export const getCloudLinkList = (params: { id: string }) => {
  return deployHttp.get<CloudLinkInfo[]>({ url: Api.GET_CLOUD_LINK_LIST, params });
};

export const createOrUpdateCloudLink = (params: CloudLinkInfo) => {
  return deployHttp.post<string>({ url: Api.CLOUD_LINK_ADD_OR_UPDATE, params });
};

export const getDetail = async (params: { id: string }) => {
  const res = await deployHttp.get<AppCardInfo>({ url: Api.GET_APP_DETAIL, params });
  if (res.ifIntegrateThirdApp === 'true') {
    res.ifIntegrateThirdApp = true;
  } else {
    res.ifIntegrateThirdApp = false;
  }
  if (res.ifIntegrateCm === 'true') {
    res.ifIntegrateCm = true;
  } else {
    res.ifIntegrateCm = false;
  }
  return res;
};
export const getCertifiedIntegration = (params: { id: string }) => {
  return deployHttp.get<CertifiedIntegration>({ url: Api.GET_CERTIFIED_INTEGRATION, params });
};
export const updateCertifiedIntegration = (params: CertifiedIntegration) => {
  return deployHttp.post<string>({ url: Api.UPDATE_CERTIFIED_INTEGRATION, params });
};
export const getSyncIntegration = (params: { id: string }) => {
  return deployHttp.get<SyncIntegration>({ url: Api.GET_SYNC_INTEGRATION, params });
};
export const updateSyncIntegration = (params: SyncIntegration) => {
  return deployHttp.post<string>({ url: Api.UPDATE_SYNC_INTEGRATION, params });
};
export const getSfsList = () => {
  return deployHttp.get({ url: Api.GET_SFS_LIST });
};

export const batchAddFs = (data: {
  appId: string;
  tenantCode: string;
  size: number;
  shareProto: 'STANDARD' | 'PERFORMANCE';
  num: number;
}) => {
  return tenantHttp.post({
    url: Api.CREATE_SFS_TURBO,
    data,
  });
};

export const deleteMirror = (id: string) => {
  return deployHttp.delete({ url: Api.DELETE_MIRROR + '/' + id });
};
